clear all
*cap log close
set more off
set seed 603


// -----------------------------------------------------
// macros for figure construction ----------
local Q = 2 
local nbin = 15

// which files are we reading in ------------
local file1 = "covar_q2"
local file2 = "recid_poly"
local file3 = "recid_cov"
// -----------------------------------------------------


// -----------------------------------------------------------------------------
// relationship between group harshness and group extrap 

/// Store slopes for printing -------
import delimited "${est}/`file1'_slope.csv", clear

// For printing ------
qui summ slope if x == "padj" & y == "y0"
local b = r(mean)
qui summ se    if x == "padj" & y == "y0"
local s = r(mean)
local py0 = "{it:{&beta}}  = `:di %4.3f `b'' (`:di %4.3f `s'')"

qui summ slope if x == "padj" & y == "ate"
local b = r(mean)
qui summ se    if x == "padj" & y == "ate"
local s = r(mean)
local pate = "{it:{&beta}}  = `:di %4.3f `b'' (`:di %4.3f `s'')"


// Read in estimates -------
import delimited "${est}/`file1'_par.csv", clear

#delimit ;
scatter y0 padj [aw=wt], msize(small) mcolor(dknavy) msymbol(Oh) || 
lfit y0 padj [aw=wt], lpattern(dash) lcolor(dknavy)
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) 
xtitle("Harshness Rate") ytitle("E(Y0)")
xlab(0.6(0.04)0.72,nogrid) ylab(0.1(0.1)0.7,nogrid)
legend(off)
text(0.65 0.62 "`py0'", place(s) size(medium)) ;
#delimit cr;
graph export "${out}/main/covar_y0.pdf", replace

#delimit ;
scatter ate padj [aw=wt], msize(small) mcolor(dknavy) msymbol(Oh) || 
lfit ate padj [aw=wt], lpattern(dash) lcolor(dknavy)
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) 
xtitle("Harshness Rate") ytitle("ATE")
xlab(0.6(0.04)0.72,nogrid) ylab(-0.05(0.025)0.025,nogrid)
legend(off)
text(0.02 0.62 "`pate'", place(s) size(medium)) ;
#delimit cr;
graph export "${out}/main/covar_ate.pdf", replace
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
// levels and gains within covariates 

// For printing ------
import delimited "${est}/`file3'.csv", clear
keep if poly == `Q'

qui summ est if par == "Y0D1" 
local b = r(mean)
qui summ se  if par == "Y0D1"
local s = r(mean)
local pt1 = "E(Y0) = `:di %4.3f `b'' (`:di %4.3f `s'')"

qui summ est if par == "ATT" 
local b = r(mean)
qui summ se  if par == "ATT"
local s = r(mean)
local pt2 = "ATT = `:di %4.3f `b'' (`:di %4.3f `s'')"

qui summ est if par == "Y0D0" 
local b = r(mean)
qui summ se  if par == "Y0D0"
local s = r(mean)
local pu1 = "E(Y0) = `:di %4.3f `b'' (`:di %4.3f `s'')"

qui summ est if par == "ATU" 
local b = r(mean)
qui summ se  if par == "ATU"
local s = r(mean)
local pu2 = "ATU = `:di %4.3f `b'' (`:di %4.3f `s'')"


// Read in ests: with cov --------
import delimited "${est}/`file3'.csv", clear
keep if poly == `Q'

* populate levels and gains ----------------
do "${code}/utility/create_levels_gains.do"
drop if mi(y0)
keep ind y0 y0u y0l y1 y1u y1l delta deltau deltal
replace ind = 0.2 if _n == 1
replace ind = 0.5  if _n == 2 
replace ind = 0.8 if _n == 3

gen baseline = 0 
tempfile part1
save    `part1'



// Read in ests: baseline -------
import delimited "${est}/`file2'.csv", clear
keep if poly == `Q'

* populate levels and gains ----------------
do "${code}/utility/create_levels_gains.do"
drop if mi(y0)
keep ind y0 y0u y0l y1 y1u y1l delta deltau deltal
replace ind = 0.2 if _n == 1
replace ind = 0.5  if _n == 2 
replace ind = 0.8 if _n == 3

gen baseline = 1
tempfile part2
save    `part2'




/// Piece together ------
use `part1', clear
qui append using `part2'
replace ind = ind-0.02 if baseline==1
replace ind = ind+0.02 if baseline==0 



#delimit ; 
twoway 
(rbar y1u y1l ind if baseline==1, color(maroon%20) barwidth(0.02))
(scatter y0 ind if baseline==1, msize(large) msymbol(Oh) mcolor(navy))
(pcarrow y0 ind y1 ind if baseline==1, lcolor(maroon) lwidth(medthick) mcolor(maroon) barbsize(medlarge))
(rbar y1u y1l ind if baseline==0, color(magenta%20) barwidth(0.02))
(scatter y0 ind if baseline==0, msize(large) msymbol(Sh) mcolor(dkgreen) lwidth(medthick))
(pcarrow y0 ind y1 ind if baseline==0, lcolor(magenta) lwidth(medthick) mcolor(magenta) barbsize(medlarge)),
xscale(r(0 1))
xlabel(0.2 "All Motorists" 0.5 "Treated" 0.8 "Untreated", nogrid) ylab(,nogrid) 
legend(ring(0) pos(2) order(2 5) cols(1) region(lstyle(border))
label(2 "Baseline") label(5 "Within Covariates")) 
xtitle("") ytitle("Pr(Reoffend)") 
graphregion(color(white)) bgcolor(white) plotregion(lcolor(black) lwidth(medthin)) 
text(0.31 0.2 "Treated:" "`pt1'" "`pt2'", place(n) size(medium))
text(0.29 0.2 "Unreated:" "`pu1'" "`pu2'", place(n) size(medium));
#delimit cr
graph export "${out}/main/covar_est.pdf", replace
// -----------------------------------------------------------------------------








